﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using Web.Api.Server.Models;
using Web.Api.Server.ViewModels;
using Web.Api.Server.ViewModels.Shops;

namespace Web.Api.Server.Controllers.Shops
{
    [SwaggerTag("前端商品接口")]
    [Route("api/shops/{shopId}/[controller]")]
    [ApiController]
    public class GoodsController : ControllerBase
    {
        private readonly ApplicationDbContext _dbContext;

        public GoodsController(ApplicationDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        [HttpGet]
        [ProducesResponseType(typeof(ResponseViewModel<IEnumerable<Goods>>), 200)]
        public async Task<IActionResult> Get(int shopId, int page = 1, int pageSize = 15)
        {
            pageSize = pageSize > 50 ? 50 : pageSize;

            var goods = await _dbContext.GoodsSet
                .Where(g => g.ShopId == shopId)
                .LeftJoin<Image>((g, i) => i.ModelName == "Goods" && i.ModelId == g.Id)
                .Page(page, pageSize)
                .ToListAsync();

            return ApiResponse.Ok(goods);
        }

        [HttpPost]
        [ProducesResponseType(typeof(ResponseViewModel<int>), 200)]
        public async Task<IActionResult> Post(int shopId, GoodsPostViewModel viewModel)
        {
            if (shopId == viewModel.ShopId)
            {
                var goods = new Goods
                {
                    Name = viewModel.Name,
                    Describe = viewModel.Describe,
                    Inventory = viewModel.Inventory,
                    Price = viewModel.Price,
                    GoodsCategoryId = viewModel.GoodsCategoryId,
                    ShopId = viewModel.ShopId,
                };
                await _dbContext.GoodsSet.AddAsync(goods);
                var result = await _dbContext.SaveChangesAsync();

                return ApiResponse.Ok(result);
            }

            return ApiResponse.NotFound();
        }

        [HttpPut("{id}")]
        [ProducesResponseType(typeof(ResponseViewModel<int>), 200)]
        public async Task<IActionResult> Put(int shopId, int id, GoodsPostViewModel viewModel)
        {
            if (shopId == viewModel.ShopId && id == viewModel.Id)
            {
                var goods = await _dbContext.GoodsSet
                    .Where(c => c.Id == viewModel.Id)
                    .FirstAsync();
                if (goods != null)
                {
                    goods.Name = viewModel.Name;
                    goods.Describe = viewModel.Describe;
                    goods.Inventory = viewModel.Inventory;
                    goods.Price = viewModel.Price;
                    goods.GoodsCategoryId = viewModel.GoodsCategoryId;
                    goods.UpdatedAt = DateTime.Now;

                    await _dbContext.GoodsSet.UpdateAsync(goods);
                    var result = await _dbContext.SaveChangesAsync();
                    return ApiResponse.Ok(result);
                }
            }

            return ApiResponse.NotFound();
        }

        [HttpDelete("{id}")]
        [ProducesResponseType(typeof(ResponseViewModel<int>), 200)]
        public async Task<IActionResult> Delete(int shopId, int id)
        {
            var goods = await _dbContext.GoodsSet
                .Where(c => c.ShopId == shopId && c.Id == id)
                .FirstAsync();
            if (goods != null)
            {
                var carts = await _dbContext.Carts
                    .Where(c => c.GoodsId == id)
                    .ToListAsync();
                _dbContext.Carts.RemoveRange(carts);

                _dbContext.GoodsSet.Remove(goods);
                var result = await _dbContext.SaveChangesAsync();
                return ApiResponse.Ok(result);
            }

            return ApiResponse.NotFound();
        }
    }
}